package pit.com.ordermobilesystem;

import org.json.JSONObject;

import com.astuetz.PagerSlidingTabStrip;

import pit.com.ordermobilesystem.fragment.ShopFragment;
import pit.com.ordermobilesystem.provider.OrderMobileProvider;
import pit.com.ordermobilesystem.sharepreference.OrderSharePreference;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends BaseFragmentActivity  {
	    ViewPager mViewPager;
	    AppSectionsPagerAdapter mAppSectionsPagerAdapter;
		LinearLayout mSetting;
	    private DrawerLayout mDrawerLayout;
	    private ActionBarDrawerToggle mDrawerToggle;
	    static String[] sections ;
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_ordermobile);
	        sections = getResources().getStringArray(R.array.section);
	        mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
	        getActionBar().setDisplayHomeAsUpEnabled(true);
	        getActionBar().setHomeButtonEnabled(true);


	        // Set up the ViewPager, attaching the adapter and setting up a listener for when the
	        // user swipes between sections.
	        mViewPager = (ViewPager) findViewById(R.id.pager);
	        mViewPager.setAdapter(mAppSectionsPagerAdapter);
	        mViewPager.setOffscreenPageLimit(4);
	        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
	            @Override
	            public void onPageSelected(int position) {

	            }
	        });
	        
	        
	        PagerSlidingTabStrip mIndicator = (PagerSlidingTabStrip) findViewById(R.id.indicator);
	        
	        mIndicator.setViewPager(mViewPager);       

	        mIndicator.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int index) {
				mViewPager.setCurrentItem(index);
				
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}
		});	
	        
	        mSetting=(LinearLayout) findViewById(R.id.setting_frame);
	        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
	        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
	                R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {

	            /** Called when a drawer has settled in a completely closed state. */
	            public void onDrawerClosed(View view) {
	                super.onDrawerClosed(view);
	                getActionBar().setTitle(getString(R.string.app_name));
	                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
	            }

	            /** Called when a drawer has settled in a completely open state. */
	            public void onDrawerOpened(View drawerView) {
	                super.onDrawerOpened(drawerView);
	                getActionBar().setTitle(getString(R.string.app_name));
	                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
	            }
	        };

	        // Set the drawer toggle as the DrawerListener
	        mDrawerLayout.setDrawerListener(mDrawerToggle);		        
	        ImageView img_login=(ImageView) mSetting.findViewById(R.id.img_login);
	        LinearLayout lay_control=(LinearLayout) mSetting.findViewById(R.id.lay_control);
	    	if(!OrderSharePreference.getUser(this).equals("")){
	    		img_login.setImageResource(R.drawable.ic_logout);
	    		lay_control.setVisibility(View.VISIBLE);
	    	}else{
	    		img_login.setImageResource(R.drawable.ic_login);
	    		lay_control.setVisibility(View.GONE);
	    	}	        
	    	img_login.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
			    	if(!OrderSharePreference.getUser(getApplicationContext()).equals("")){
			    		OrderSharePreference.setUser(getApplicationContext(), "");
			    		TextView tv_login=(TextView) mSetting.findViewById(R.id.tv_login);
			    		tv_login.setText(getString(R.string.login));
			    	}else{
						Intent intent=new Intent(MainActivity.this,LoginActivity.class);
						startActivity(intent);
			    	}					

				}
			});
	        
	    }
	    
	    @Override
		public void notifyLogin(JSONObject parameter) {
	        TextView tv_login=(TextView) mSetting.findViewById(R.id.tv_login);
    		LinearLayout lay_control=(LinearLayout) mSetting.findViewById(R.id.lay_control);
	    	if(!OrderSharePreference.getUser(this).equals("")){
	    		tv_login.setText(getString(R.string.logout));
	    		lay_control.setVisibility(View.VISIBLE);
	    	}else{
	    		tv_login.setText(getString(R.string.login));
	    		lay_control.setVisibility(View.GONE);
	    	}
		}	    
	    
	    /**
	     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
	     * sections of the app.
	     */
	    public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
		    int[] business_type={2,3,4,1};
	        public AppSectionsPagerAdapter(FragmentManager fm) {
	            super(fm);
	        }

	        @Override
	        public Fragment getItem(int i) {
	                    Fragment fragment = new ShopFragment();
	                    Bundle args = new Bundle();
	                    args.putInt("type_business",business_type[i]);
	                    fragment.setArguments(args);
	                    return fragment;

	        }

	        @Override
	        public int getCount() {
	            return 4;
	        }

	        @Override
	        public CharSequence getPageTitle(int position) {
	        
	            return sections[position];
	        }
	    }




	    
	    @Override
	    protected void onPostCreate(Bundle savedInstanceState) {
	        super.onPostCreate(savedInstanceState);
	        // Sync the toggle state after onRestoreInstanceState has occurred.
	        mDrawerToggle.syncState();
	    }

	    @Override
	    public void onConfigurationChanged(Configuration newConfig) {
	        super.onConfigurationChanged(newConfig);
	        mDrawerToggle.onConfigurationChanged(newConfig);
	    }

	    @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		openSetting();

	        return super.onOptionsItemSelected(item);
	    }
	    
	    public void openSetting(){
		    	if(mDrawerLayout.isDrawerOpen(mSetting)){
					mDrawerLayout.closeDrawer(mSetting);
			}else{
					mDrawerLayout.openDrawer(mSetting);
					
			}
	    }
	    
	    @Override
	    public boolean onCreateOptionsMenu(Menu menu) {
	        getMenuInflater().inflate(R.menu.menu_ordermobile, menu);
	        return super.onCreateOptionsMenu(menu);
	    }


	    
}
